package com.googlecode.jmicroerp.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author alex
 */
@Entity
@Table(name = "bonuri")
@NamedQueries({
    @NamedQuery(
            name = "Bon.queryBonuriProduseByMagazin",
            query = "select mag.denumire, prd.produs.denumire, sum(prd.cantitate), prd.produs.pret, sum(prd.cantitate)*prd.produs.pret "
            + "from Bon bon "
            + "join bon.magazin mag "
            + "join bon.produse prd "
            + "where bon.dataVanzarii >= ?1 and bon.dataVanzarii < ?2 "
            + "and mag.id = ?3 "
            + "group by prd.produs "
            + "order by prd.produs.ordine asc"),
    @NamedQuery(
            name = "Bon.queryCantitateProduseByMagazin",
            query = "select sum(prd.cantitate) "
            + "from Bon bon "
            + "join bon.magazin mag "
            + "join bon.produse prd "
            + "where bon.dataVanzarii > ?1 and mag.id = ?2 and prd.produs.id = ?3 "
            + "group by prd.produs"),
   @NamedQuery(       
            name = "Bon.queryCantitateProduseByMagazinByDay",
            query = "select sum(prd.cantitate) "
            + "from Bon bon "
            + "join bon.magazin mag "
            + "join bon.produse prd "
            + "where mag.id = ?1 and prd.produs.id = ?2 "
            + "and (bon.dataVanzarii >= ?3 and bon.dataVanzarii < ?4) "
            + "group by prd.produs"),
    @NamedQuery(
            name = "Bon.queryBonuriByMagazin",
            query = "select mag.denumire, count(bon), sum(bon.valoareTotala) "
            + "from Bon bon join bon.magazin mag "
            + "where bon.dataVanzarii > ?1 "
            + "group by mag.denumire"),
    @NamedQuery(
            name = "Bon.queryBonuri",
            query = "select count(bon), sum(bon.valoareTotala) "
            + "from Bon bon "
            + "where bon.dataVanzarii > ?1 "),
    @NamedQuery(
            name = "Bon.queryVanzariAzi",
            query = "select sum(bon.valoareTotala) "
            + "from Bon bon "
            + "where bon.dataVanzarii > ?1 "),
    @NamedQuery(
            name = "Bon.queryVanzariAziByMagazin",
            query = "select sum(bon.valoareTotala) "
            + "from Bon bon join bon.magazin mag "
            + "where bon.dataVanzarii > ?1 and mag.id = ?2 "
            + "group by mag.denumire"),
    @NamedQuery(
            name = "Bon.queryVanzariByMagazin",
            query = "select sum(bon.valoareTotala) "
            + "from Bon bon "
            + "join bon.magazin mag "
            + "where bon.dataVanzarii >= ?1 and bon.dataVanzarii < ?2 "
            + "and mag.id = ?3 "
            + "group by mag.denumire")
})
public class Bon extends AbstractEntitate {

    private Magazin magazin;
    private List<ProdusVandut> produse;
    private Utilizator utilizator;
    private Date dataVanzarii;
    private BigDecimal valoareTotala;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MAGAZIN_ID")
    public Magazin getMagazin() {
        return magazin;
    }

    public void setMagazin(Magazin magazin) {
        this.magazin = magazin;
    }

    @ManyToMany
    public List<ProdusVandut> getProduse() {
        return produse;
    }

    public void setProduse(List<ProdusVandut> produse) {
        this.produse = produse;
    }

    @Column(name = "DATA_VANZARII")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDataVanzarii() {
        return dataVanzarii;
    }

    public void setDataVanzarii(Date dataVanzarii) {
        this.dataVanzarii = dataVanzarii;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "UTILIZATOR_ID")
    public Utilizator getUtilizator() {
        return utilizator;
    }

    public void setUtilizator(Utilizator utilizator) {
        this.utilizator = utilizator;
    }

    @Column(name = "VALOARE_TOTALA")
    public BigDecimal getValoareTotala() {
        return valoareTotala;
    }

    public void setValoareTotala(BigDecimal valoareTotala) {
        this.valoareTotala = valoareTotala;
    }
}
